package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblehttp;

import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblelogger.FundibleASLger;
import com.test.fundible.fundible_00_utils.FundibleUtil;

import java.util.concurrent.CompletableFuture;

public class FundibleASReqstManger {

    private final static String TAG = "ASRequestManager";
    public String fc52="";
    private final String url;
    public String l4cf="";
    public String pd2J="";
    public String gs4T="";
    public String svg1="";
    public String w393="";
    private final String body;
    private final String reqId;

    public String oTHN="";
    public char wrY3='k';
    public boolean hQOe=true;
    public float nPuc=0.49019384f;
    public int qX5q=107;
    public String fz2X="";

    private final OnResponseListener_fundible onResponseListener;

    private void test(){
        if (qX5q<0) {
            setFz2X(String.valueOf(nPuc));
            setWrY3((char) 56);
            setoTHN("ghiwejh");
        }
    }

    private FundibleASReqstManger(Builder_fundible builder) {
        this.url = builder.url;
        this.body = builder.body;
        this.reqId = builder.reqId;
        this.onResponseListener = builder.onResponseListener;
    }

    public void doPostRqst_fundible() {
        CompletableFuture<FundibleASResp> ntwkTaskSupplier = CompletableFuture.supplyAsync(new FundibleASPostAsyncTsk(this.reqId, this.url, this.body));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(ntwkTaskSupplier);

        allOfFuture.thenRunAsync(() -> {
            FundibleASResp resultObj = ntwkTaskSupplier.join();
            String code = resultObj.result.optString("code");

            // 日志标识
            StringBuilder stringBuffer = new StringBuilder();
            stringBuffer.append("result url: ").append(this.url);
            stringBuffer.append("\n");
            stringBuffer.append("result task id: ").append(resultObj.taskId);
            stringBuffer.append("\n");
            stringBuffer.append("result obj: ").append(resultObj.result);
            stringBuffer.append("\n");
            stringBuffer.append("result code: ").append(code);
            stringBuffer.append("\n");
            if (Integer.parseInt(code) == 0) {
                stringBuffer.append("result success");
            } else {
                stringBuffer.append("result error");
            }
            stringBuffer.append("\n");
            stringBuffer.append("==========");
            FundibleASLger.d_fundible(TAG, stringBuffer.toString());

            // 主线程回调
            FundibleUtil.safeRun(() -> {
                if (this.onResponseListener != null) {
                    this.onResponseListener.onResponse(resultObj);
                }
            });
        });
    }

    public static class Builder_fundible {

        private String reqId;
        private String url;
        private String body;

        private OnResponseListener_fundible onResponseListener;

        public Builder_fundible setReqId_fundible(String reqId) {
            this.reqId = reqId;
            return this;
        }

        public Builder_fundible setUrl_fundible(String url) {
            this.url = url;
            return this;
        }

        public Builder_fundible setBody_fundible(String body) {
            this.body = body;
            return this;
        }

        public Builder_fundible setOnResponseListener_fundible(OnResponseListener_fundible listener) {
            this.onResponseListener = listener;
            return this;
        }

        public FundibleASReqstManger build_fundible() {
            return new FundibleASReqstManger(this);
        }
    }

    public interface OnResponseListener_fundible {
        void onResponse(FundibleASResp result);
    }

    public void setFc52(String fc52) {
        this.fc52 = fc52;
    }

    public void setL4cf(String l4cf) {
        this.l4cf = l4cf;
    }

    public void setPd2J(String pd2J) {
        this.pd2J = pd2J;
    }

    public void setGs4T(String gs4T) {
        this.gs4T = gs4T;
    }

    public void setSvg1(String svg1) {
        this.svg1 = svg1;
    }

    public void setW393(String w393) {
        this.w393 = w393;
    }

    public String getFc52() {
        return fc52;
    }

    public String getUrl() {
        return url;
    }

    public String getL4cf() {
        return l4cf;
    }

    public String getPd2J() {
        return pd2J;
    }

    public String getGs4T() {
        return gs4T;
    }

    public String getSvg1() {
        return svg1;
    }

    public String getW393() {
        return w393;
    }

    public String getBody() {
        return body;
    }

    public String getReqId() {
        return reqId;
    }

    public OnResponseListener_fundible getOnResponseListener() {
        return onResponseListener;
    }

    public void setoTHN(String oTHN) {
        this.oTHN = oTHN;
    }

    public void setWrY3(char wrY3) {
        this.wrY3 = wrY3;
    }

    public void sethQOe(boolean hQOe) {
        this.hQOe = hQOe;
    }

    public void setnPuc(float nPuc) {
        this.nPuc = nPuc;
    }

    public void setqX5q(int qX5q) {
        this.qX5q = qX5q;
    }

    public void setFz2X(String fz2X) {
        this.fz2X = fz2X;
    }
}
